<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>正则 | sponge</title>
    <meta name="description" content="一些很随意的前端笔记">
    <link rel="icon" href="/vuepress-blog/hero.jpg">
    
    <link rel="preload" href="/vuepress-blog/assets/css/0.styles.5d74642e.css" as="style"><link rel="preload" href="/vuepress-blog/assets/js/app.21385443.js" as="script"><link rel="preload" href="/vuepress-blog/assets/js/10.b2b9db1b.js" as="script"><link rel="prefetch" href="/vuepress-blog/assets/js/11.fafc0e1d.js"><link rel="prefetch" href="/vuepress-blog/assets/js/12.a3867a96.js"><link rel="prefetch" href="/vuepress-blog/assets/js/13.8b18fa10.js"><link rel="prefetch" href="/vuepress-blog/assets/js/2.2e3e2c34.js"><link rel="prefetch" href="/vuepress-blog/assets/js/3.dc26014a.js"><link rel="prefetch" href="/vuepress-blog/assets/js/4.05a9910c.js"><link rel="prefetch" href="/vuepress-blog/assets/js/5.ffffdb2b.js"><link rel="prefetch" href="/vuepress-blog/assets/js/6.334e70a7.js"><link rel="prefetch" href="/vuepress-blog/assets/js/7.19b72361.js"><link rel="prefetch" href="/vuepress-blog/assets/js/8.2f20ced6.js"><link rel="prefetch" href="/vuepress-blog/assets/js/9.624d7ce4.js">
    <link rel="stylesheet" href="/vuepress-blog/assets/css/0.styles.5d74642e.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/vuepress-blog/" class="home-link router-link-active"><!----> <span class="site-name">sponge</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">前端技术栈</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-blog/jsNotes/reflow.html" class="nav-link">笔记</a></li><li class="dropdown-item"><!----> <a href="/vuepress-blog/tips/arrSort.html" class="nav-link">其他</a></li></ul></div></div><div class="nav-item"><a href="/vuepress-blog/about.html" class="nav-link">关于我们</a></div><div class="nav-item"><a href="https://github.com/spongege" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">前端技术栈</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-blog/jsNotes/reflow.html" class="nav-link">笔记</a></li><li class="dropdown-item"><!----> <a href="/vuepress-blog/tips/arrSort.html" class="nav-link">其他</a></li></ul></div></div><div class="nav-item"><a href="/vuepress-blog/about.html" class="nav-link">关于我们</a></div><div class="nav-item"><a href="https://github.com/spongege" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><div class="sidebar-group first collapsable"><p class="sidebar-heading open"><span>jsNotes</span> <span class="arrow down"></span></p> <ul class="sidebar-group-items"><li><a href="/vuepress-blog/jsNotes/reflow.html" class="sidebar-link">回流（reflow）与重绘(repaint)</a></li><li><a href="/vuepress-blog/jsNotes/reg.html" class="active sidebar-link">正则</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/vuepress-blog/jsNotes/reg.html#_1、直接量字符" class="sidebar-link">1、直接量字符</a></li><li class="sidebar-sub-header"><a href="/vuepress-blog/jsNotes/reg.html#_2、字符类" class="sidebar-link">2、字符类</a></li><li class="sidebar-sub-header"><a href="/vuepress-blog/jsNotes/reg.html#_3、重复" class="sidebar-link">3、重复</a></li><li class="sidebar-sub-header"><a href="/vuepress-blog/jsNotes/reg.html#_4、选择、分组和引用" class="sidebar-link">4、选择、分组和引用</a></li><li class="sidebar-sub-header"><a href="/vuepress-blog/jsNotes/reg.html#_5、指定匹配位置" class="sidebar-link">5、指定匹配位置</a></li><li class="sidebar-sub-header"><a href="/vuepress-blog/jsNotes/reg.html#_6、修饰符" class="sidebar-link">6、修饰符</a></li><li class="sidebar-sub-header"><a href="/vuepress-blog/jsNotes/reg.html#_7、用于模式匹配的-string-方法" class="sidebar-link">7、用于模式匹配的 String 方法</a></li><li class="sidebar-sub-header"><a href="/vuepress-blog/jsNotes/reg.html#es6-正则的扩展" class="sidebar-link">ES6 正则的扩展</a></li><li class="sidebar-sub-header"><a href="/vuepress-blog/jsNotes/reg.html#一些好用的正则" class="sidebar-link">一些好用的正则</a></li></ul></li><li><a href="/vuepress-blog/jsNotes/arrSort.html" class="sidebar-link">数组排序</a></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="正则"><a href="#正则" aria-hidden="true" class="header-anchor">#</a> 正则</h1> <h2 id="_1、直接量字符"><a href="#_1、直接量字符" aria-hidden="true" class="header-anchor">#</a> 1、直接量字符</h2> <h3 id="_1-1、正则表达式中的直接量字符"><a href="#_1-1、正则表达式中的直接量字符" aria-hidden="true" class="header-anchor">#</a> 1-1、正则表达式中的直接量字符</h3> <table><thead><tr><th>字符</th> <th>匹配</th></tr></thead> <tbody><tr><td>字母和数字字符</td> <td>自身</td></tr> <tr><td>\o</td> <td>NUL 字符(\u0000)</td></tr> <tr><td>\f</td> <td>换页符,等价于\x0c 和\cL</td></tr> <tr><td>\v</td> <td>垂直制表符,等价于\x0b 和\cK</td></tr> <tr><td>\t</td> <td>制表符,等价于\x09 和\cl</td></tr> <tr><td>\S</td> <td>任何非空白字符，等价于[^\f\n\r\t\v]</td></tr> <tr><td>\n</td> <td>换行符，等价于\x0a 和\cJ</td></tr> <tr><td>\cx</td> <td>匹配由 x 指明的控制字符。例如， \cM 匹配一个 Control-M 或回车符。x 的值必须为 A-Z 或 a-z 之一。否则，将 c 视为一个原义的 'c' 字符。</td></tr> <tr><td>\s</td> <td>匹配任何空白字符，包括空格、制表符、换页符等等。等价于 [\f\n\r\t\v]。注意 Unicode 正则表达式会匹配全角空格符。</td></tr> <tr><td>\r</td> <td>匹配一个回车符。等价于 \x0d 和 \cM。</td></tr></tbody></table> <h3 id="_1-1、需要转义的字符"><a href="#_1-1、需要转义的字符" aria-hidden="true" class="header-anchor">#</a> 1-1、需要转义的字符</h3> <p>正则表达式中，许多标点符号具有特殊含义，如果使用需要转义。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token operator">^</span>$<span class="token punctuation">.</span><span class="token operator">*</span><span class="token operator">+</span><span class="token operator">?</span><span class="token operator">!</span><span class="token punctuation">:</span><span class="token operator">|</span>\<span class="token operator">/</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre></div><h2 id="_2、字符类"><a href="#_2、字符类" aria-hidden="true" class="header-anchor">#</a> 2、字符类</h2> <ul><li>将直接量字符单独放进方括号就组成了字符类。</li> <li>通过^来定义否定字符类，用来匹配所有不包含再方括号中的字符</li> <li>通过连字符来表示字符范围</li></ul> <p>如下：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token operator">/</span><span class="token punctuation">[</span>abc<span class="token punctuation">]</span><span class="token operator">/</span>     <span class="token comment">//和字母a,b,c中任意一个都匹配。</span>
<span class="token operator">/</span><span class="token punctuation">[</span><span class="token operator">^</span>abc<span class="token punctuation">]</span><span class="token operator">/</span>    <span class="token comment">//匹配a,b,c之外的所有字符</span>
<span class="token operator">/</span><span class="token punctuation">[</span>a<span class="token operator">-</span>z<span class="token punctuation">]</span><span class="token operator">/</span> <span class="token operator">/</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">/</span> <span class="token comment">//分别匹配a-z的小写，0-9.</span>
<span class="token operator">/</span><span class="token punctuation">[</span>a<span class="token operator">-</span>zA<span class="token operator">-</span><span class="token constant">Z0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">/</span>   <span class="token comment">//匹配任何数字和字母</span>
</code></pre></div><p>正则表达式的字符类</p> <table><thead><tr><th>字符</th> <th>匹配</th></tr></thead> <tbody><tr><td>[...]</td> <td>方括号中的任意字符</td></tr> <tr><td>[^...]</td> <td>除了方括号中的任意字符</td></tr> <tr><td>.</td> <td>除了换行符和其他 Unicode 行终止符之外的任意字符</td></tr> <tr><td>\w</td> <td>任何的 ASCII 字符组成的单词，等价于[a-zA-Z0-9]</td></tr> <tr><td>\W</td> <td>任何的不是 ASCII 字符组成的单词，等价于[^a-za-z0-9]</td></tr> <tr><td>\s</td> <td>任何 Unicode 空白符</td></tr> <tr><td>\S</td> <td>任何非 Unicode 空白符的字符，注意\w 和\S 不同</td></tr> <tr><td>\d</td> <td>任何 ASCII 数字，等价于[0-9]</td></tr> <tr><td>\D</td> <td>除了 ASCII 数字之外的任何字符，等价于[^0-9]</td></tr> <tr><td>[\b]</td> <td>退格直接量（特例）</td></tr></tbody></table> <h2 id="_3、重复"><a href="#_3、重复" aria-hidden="true" class="header-anchor">#</a> 3、重复</h2> <p>正则表达式中的重复字符语法</p> <table><thead><tr><th>字符</th> <th>含义</th></tr></thead> <tbody><tr><td>{n,m}</td> <td>匹配前一项至少 n 次，但不能超过 m 次</td></tr> <tr><td>{n,}</td> <td>匹配前一项 n 次或者更多次</td></tr> <tr><td>{n}</td> <td>匹配前一项 n 次</td></tr> <tr><td>?</td> <td>匹配前一项 0 次或者 1 次，等价于{0,1}</td></tr> <tr><td>+</td> <td>匹配前一项 1 次或多次，等价{1,}</td></tr> <tr><td>*</td> <td>匹配前一项 0 次或者多次，等价{0,}</td></tr></tbody></table> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token operator">/</span>\d<span class="token punctuation">{</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">}</span><span class="token operator">/</span>      <span class="token comment">//匹配2-4个数字</span>
<span class="token operator">/</span>\w<span class="token punctuation">{</span><span class="token number">3</span><span class="token punctuation">}</span>\d<span class="token operator">?</span><span class="token operator">/</span>      <span class="token comment">//精确匹配三个丹斯和一个可选的数字</span>
<span class="token operator">/</span>\s<span class="token operator">+</span>javascript\s<span class="token operator">+</span><span class="token operator">/</span>    <span class="token comment">//匹配前后带一个或者多个空格的字符串javascript</span>
<span class="token operator">/</span><span class="token punctuation">[</span><span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">]</span><span class="token operator">*</span><span class="token operator">/</span>             <span class="token comment">//匹配一个或者多个非左括号的字符</span>
</code></pre></div><h3 id="非贪婪的重复"><a href="#非贪婪的重复" aria-hidden="true" class="header-anchor">#</a> 非贪婪的重复</h3> <p>上面的表中列出的匹配重复字符是尽可能多的匹配，而且允许后续的正则表达式继续匹配，因此被称为贪婪匹配。只需再待匹配的字符后跟随一个问好即可实现非贪婪匹配。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token operator">/</span>a<span class="token operator">+</span><span class="token operator">/</span>    <span class="token comment">//如果匹配aaa,会匹配三个字符aaa</span>
<span class="token operator">/</span>a<span class="token operator">+</span><span class="token operator">?</span><span class="token operator">/</span>   <span class="token comment">//如果匹配aaa,只会匹配一个字符a</span>
<span class="token comment">//匹配aaab时</span>
<span class="token operator">/</span>a<span class="token operator">+</span>b<span class="token operator">/</span>       <span class="token comment">//会匹配出aaab</span>
<span class="token operator">/</span>a<span class="token operator">+</span><span class="token operator">?</span>b<span class="token operator">/</span>      <span class="token comment">//同样会匹配出aaab 因为正则匹配，总是从能够匹配的第一个字符开始</span>
</code></pre></div><h2 id="_4、选择、分组和引用"><a href="#_4、选择、分组和引用" aria-hidden="true" class="header-anchor">#</a> 4、选择、分组和引用</h2> <p>字符 &quot;|&quot; 用来分割供选择的字符。匹配规则从左到右，如果匹配到，即使后面的更更符合也不会再去匹配</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token operator">/</span>ab<span class="token operator">|</span>cd<span class="token operator">|</span>ef<span class="token operator">/</span>   <span class="token comment">//可以匹配字符串ab,cd,ef的任意一种情况</span>
<span class="token operator">/</span>\d<span class="token punctuation">{</span><span class="token number">3</span><span class="token punctuation">}</span><span class="token operator">|</span><span class="token punctuation">[</span>a<span class="token operator">-</span>z<span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token number">4</span><span class="token punctuation">}</span><span class="token operator">/</span>    <span class="token comment">//用来匹配三个数字或者四个小数</span>
<span class="token operator">/</span>a<span class="token operator">|</span>ab<span class="token operator">/</span>              <span class="token comment">//如果匹配ab之后匹配到a</span>
</code></pre></div><h3 id="圆括号的作用（一）"><a href="#圆括号的作用（一）" aria-hidden="true" class="header-anchor">#</a> 圆括号的作用（一）</h3> <p>把单独的项组合成子表达式，以便可以像处理一个单独的单元那样用 &quot;|&quot;、&quot;*&quot;、&quot;+&quot;和&quot;?&quot;等来对单元中的项进行处理</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token operator">/</span><span class="token function">java</span><span class="token punctuation">(</span>script<span class="token punctuation">)</span><span class="token operator">?</span><span class="token operator">/</span>     <span class="token comment">//匹配字符串&quot;java&quot;，其后可以有&quot;script&quot;也可以没有</span>
<span class="token operator">/</span><span class="token punctuation">(</span>ab<span class="token operator">|</span>cd<span class="token punctuation">)</span><span class="token operator">+</span><span class="token operator">|</span>ef<span class="token operator">/</span>       <span class="token comment">//可以匹配字符串 &quot;ef&quot;,也可以匹配字符串&quot;ab&quot;和&quot;cd&quot;的一次或多次重复</span>
</code></pre></div><h3 id="圆括号的作用（二）"><a href="#圆括号的作用（二）" aria-hidden="true" class="header-anchor">#</a> 圆括号的作用（二）</h3> <p>在完整的模式中定义子模式。在一个正则表达式成功的和目标字符串像匹配时，可以从目标中
抽出和圆括号中的子模式相匹配的部分。</p> <p>假如需要检索一个或多个小写字母后面跟随意味或多位数字，但是我们比较关系后面的数字,可以像下面一样。后面会详细介绍重匹配中抽取数字。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token regex">/[a-z]+\d+/</span>
<span class="token regex">/[a-z]+(\d+)/</span>
</code></pre></div><h3 id="圆括号的作用（三）"><a href="#圆括号的作用（三）" aria-hidden="true" class="header-anchor">#</a> 圆括号的作用（三）</h3> <p>允许同一个正则表达式的后面引用前面的子表达式。通过在字符 &quot;&quot; 后面加以为或者多为数字来实现。这个数字制定了待圆括号的子表达式在正则总的位置。如 \1 引用第一个带圆括号的子表达式 \3 就是第三个带圆括号的子表达式</p> <p><strong>对正则表达式的前一个子表达式的引用，并不是对子表达式模式的引用，而是指与那个模式相匹配的文本的引用</strong></p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">//下面的正则表达式匹配位于单引号或双引号之内的0个或者多个字符。但是，他们并不要求左侧和右侧的引号匹配(即，加入的两个引号都是单引号或都属双引号);</span>
<span class="token operator">/</span><span class="token punctuation">[</span><span class="token string">'&quot;][^'</span><span class="token string">&quot;]*['&quot;</span><span class="token punctuation">]</span><span class="token operator">/</span>
<span class="token operator">/</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'&quot;])[^'</span>&quot;<span class="token punctuation">]</span><span class="token operator">*</span>\<span class="token number">1</span><span class="token operator">/</span>    <span class="token comment">//此时就要求前后的引号一样</span>
</code></pre></div><p>\1 匹配的第一个带圆括号的子表达式所匹配的模式。在这个例子中，存在这样一条约束，那就是左侧的引号必须和右侧的引号像匹配。正则表达 hi 是不允许用双引号括起来的内用中有单引号，反之亦然。不能在字符类中使用下面的这种引用</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token regex">/(['&quot;])[^\1]*\1/</span><span class="token punctuation">;</span> <span class="token comment">//这种写法非法</span>
</code></pre></div><p>如果使用 (?:...) 分组的表达式,代表不记忆与该项想匹配的字符串</p> <h3 id="正则表达式的选择、分组和引用字符"><a href="#正则表达式的选择、分组和引用字符" aria-hidden="true" class="header-anchor">#</a> 正则表达式的选择、分组和引用字符</h3> <table><thead><tr><th>字符</th> <th>含义</th></tr></thead> <tbody><tr><td></td> <td>选择，匹配到该符合左边的子表达式或者右边的子表达式</td></tr> <tr><td>(...)</td> <td>组合，将几个项组合成一个单元，这个单元可以通过 &quot;*&quot;、&quot;+&quot;、&quot;?&quot;和&quot;</td></tr> <tr><td>(?:...)</td> <td>只组合，把项组合到一个单元，但不记忆与该项相匹配的字符串</td></tr> <tr><td>\n</td> <td>和第 n 个分组第一次匹配到的字符串想匹配，组是圆括号中的子表达式(也有可能是嵌套的)，组索引是从左到右的左括号数，以&quot;(?:&quot;形式的分组不编码</td></tr></tbody></table> <h2 id="_5、指定匹配位置"><a href="#_5、指定匹配位置" aria-hidden="true" class="header-anchor">#</a> 5、指定匹配位置</h2> <h3 id="正则表达式中的锚字符"><a href="#正则表达式中的锚字符" aria-hidden="true" class="header-anchor">#</a> 正则表达式中的锚字符</h3> <table><thead><tr><th>字符</th> <th>含义</th></tr></thead> <tbody><tr><td>^</td> <td>匹配字符串的开头，在多行检索中，匹配一行的开头</td></tr> <tr><td>$</td> <td>匹配字符串的结尾，在多行检索中，匹配一个行的结尾</td></tr> <tr><td>\b</td> <td>匹配一个单词的边界，简而言之，就是位于字符\w 和\W 之间的位置，或者位于字符\w 和字符串的开头或者结尾之间的位置</td></tr> <tr><td>\B</td> <td>匹配非单词边界的位置</td></tr> <tr><td>(?=p)</td> <td>零宽正向先行断言，要求接下来的字符都与 p 匹配，但不能包括匹配 p 的那些字符</td></tr> <tr><td>(?!p)</td> <td>零宽负向先行断言，要求接下来的字符不与 p 匹配</td></tr></tbody></table> <h3 id="边界"><a href="#边界" aria-hidden="true" class="header-anchor">#</a> 边界</h3> <p>具体如下：
/^JavaScript$/用来匹配 JavaScript。但如果想匹配 Java 这个单词本身，可以使用正则表达式/\sJava\s/,可以匹配到前后都有空格的单词 &quot;Java&quot; 。但是存在问题</p> <ol><li>如果&quot;Java&quot;出啊先在字符串的开始或者结尾，就匹配不成功，除非开始和结尾处各有一个空格</li> <li>当找到与之匹配的字符串时，它返回的匹配字符串的前端和后端都有空格。</li></ol> <p>因此可以使用单词的表姐\b 来代替真正的空格符 \s 进行匹配。\B 匹配锚点党委不是单词边界之处。
如下：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token regex">/\B[Sscript]/</span>
<span class="token operator">/</span>\<span class="token constant">B</span><span class="token punctuation">[</span>Ss<span class="token punctuation">]</span>cript<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'postscript'</span><span class="token punctuation">)</span>    <span class="token comment">//true</span>
<span class="token operator">/</span>\<span class="token constant">B</span><span class="token punctuation">[</span>Ss<span class="token punctuation">]</span>cript<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'javascript'</span><span class="token punctuation">)</span>    <span class="token comment">//true</span>
<span class="token operator">/</span>\<span class="token constant">B</span><span class="token punctuation">[</span>Ss<span class="token punctuation">]</span>cript<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'script'</span><span class="token punctuation">)</span>        <span class="token comment">//false</span>
<span class="token operator">/</span>\<span class="token constant">B</span><span class="token punctuation">[</span>Ss<span class="token punctuation">]</span>cript<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'Scripting'</span><span class="token punctuation">)</span>     <span class="token comment">//false</span>
</code></pre></div><h3 id="先行断言"><a href="#先行断言" aria-hidden="true" class="header-anchor">#</a> 先行断言</h3> <p>任意正则表达式都可以作为锚点条件。如果在符号 &quot;(?=&quot; 和 &quot;)&quot;之间加入一个表达式，它就是一个<strong>先行断言</strong>。用来说明圆括号总的表达式必须正确匹配，但并不是真正意义上的匹配。如下</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token regex">/[Jj]ava([Ss]cript)?(?=\:)/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'javascript:console.log()'</span><span class="token punctuation">)</span>    <span class="token comment">//true</span>
<span class="token operator">/</span><span class="token punctuation">[</span>Jj<span class="token punctuation">]</span><span class="token function">ava</span><span class="token punctuation">(</span><span class="token punctuation">[</span>Ss<span class="token punctuation">]</span>cript<span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator">=</span>\<span class="token punctuation">:</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'java System.out'</span><span class="token punctuation">)</span>             <span class="token comment">//false 因为没有匹配到:</span>
</code></pre></div><h3 id="负向先行断言"><a href="#负向先行断言" aria-hidden="true" class="header-anchor">#</a> 负向先行断言</h3> <p>带有 &quot;(:!&quot;的断言是负向先行断言，用来指定接下来的字符都不必匹配。如下：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token regex">/Java(?!Script)([A-Z]\w*)/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">&quot;JavaScript&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//false</span>
<span class="token comment">//匹配任意的Java后面跟随一个大写字母和任意多个ASCII单词都为true</span>
</code></pre></div><h2 id="_6、修饰符"><a href="#_6、修饰符" aria-hidden="true" class="header-anchor">#</a> 6、修饰符</h2> <p>语法的最后一个知识点，用来说明高级匹配模式的规则。</p> <table><thead><tr><th>字符</th> <th>含义</th></tr></thead> <tbody><tr><td>i</td> <td>执行不区分大小写的匹配</td></tr> <tr><td>g</td> <td>执行全局匹配，即找到所有的匹配，而不是找到第一个之后就停止</td></tr> <tr><td>m</td> <td>多行匹配模式，^匹配一行的开头和字符串的开头，$匹配行的结束和字符串的结束</td></tr></tbody></table> <h2 id="_7、用于模式匹配的-string-方法"><a href="#_7、用于模式匹配的-string-方法" aria-hidden="true" class="header-anchor">#</a> 7、用于模式匹配的 String 方法</h2> <h3 id="search"><a href="#search" aria-hidden="true" class="header-anchor">#</a> search()</h3> <p>参数时一个正则表达式，返回第一个与之匹配的字符串的其实位置，如果找不到就返回-1</p> <p><strong>注意：</strong>
search 方法的参数如果不是正则表达式，则会通过 RegExp 构造函数将它转换成正则表达式。并且不支持全局检索，会自动忽略修饰符 g</p> <h3 id="replace"><a href="#replace" aria-hidden="true" class="header-anchor">#</a> replace()</h3> <p>第一个参数时正则表达式，第二个参数是要进行替换的字符串。
替换的字符串可以插入下面特殊的变量名</p> <table><thead><tr><th>变量名</th> <th>代表的值</th></tr></thead> <tbody><tr><td>$$</td> <td>插入一个 &quot;$&quot;。</td></tr> <tr><td>$&amp;</td> <td>插入匹配的子串。</td></tr> <tr><td>$`</td> <td>插入当前匹配的子串左边的内容。</td></tr> <tr><td>$'</td> <td>插入当前匹配的子串右边的内容。</td></tr> <tr><td>$n</td> <td>假如第一个参数是 RegExp 对象，并且 n 是个小于 100 的非负整数，那么插入第 n 个括号匹配的字符串。提示：索引是从 1 开始</td></tr></tbody></table> <p>具体使用如下:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ant,zone&quot;</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/(\w+),(\w+)/</span><span class="token punctuation">,</span> <span class="token string">&quot;$2 $1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//zone ant</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ant88&quot;</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/(\d+)/</span><span class="token punctuation">,</span> <span class="token string">&quot;$1 $&amp;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//ant88 88</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ant88&quot;</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/(\d+)/</span><span class="token punctuation">,</span> <span class="token string">&quot;$1$`&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//ant88ant  $`匹配左边的ant</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;88ant&quot;</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/(\d+)/</span><span class="token punctuation">,</span> <span class="token string">&quot;$1$'&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//88antant  $'匹配到右边的ant</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;88ant&quot;</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/(\d+)/</span><span class="token punctuation">,</span> <span class="token string">&quot;$$&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//$ant</span>
</code></pre></div><p>replace 的其他使用见 MDN</p> <h3 id="match"><a href="#match" aria-hidden="true" class="header-anchor">#</a> match()</h3> <p>接受一个正则表达式，返回一个有匹配结果组成的数组，如果正则表达式设置了修饰符 g,则返回的数组包含字符串中的所有匹配结果。</p> <p>接受的参数分为三种情况：</p> <ol><li>如果正则带有全局的修饰符 g 且没有分组(圆括号)时，返回的数组第一个参数时匹配到的值，同时还有其他的属性</li> <li>如果正则中含有全局修饰符 g，返回的时包含所有匹配到结果的数组</li> <li>如果含有分组(圆括号)是，返回的第一个参数是完整的匹配，后面存放的是圆括号括起来表达式相匹配的子串。</li></ol> <div class="language-javascript extra-class"><pre class="language-javascript"><code>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;1plus2equals3&quot;</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex">/\d/</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//[&quot;1&quot;, index: 0, input: &quot;1plus2equals3&quot;, groups: undefined]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;1plus2equals3&quot;</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex">/\d/g</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//[&quot;1&quot;, &quot;2&quot;, &quot;3&quot;]</span>

<span class="token keyword">var</span> url <span class="token operator">=</span> <span class="token regex">/(\w+):\/\/([\w.]+)\/(\S*)/</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> text <span class="token operator">=</span> <span class="token string">&quot;Visit  my blog at http://www.example.com/david&quot;</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>text<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [&quot;http://www.example.com/david&quot;, &quot;http&quot;, &quot;www.example.com&quot;, &quot;david&quot;, index: 18, input: &quot;Visit  my blog at http://www.example.com/david&quot;, groups: undefined]</span>
</code></pre></div><h3 id="exec"><a href="#exec" aria-hidden="true" class="header-anchor">#</a> exec()</h3> <p>和 match 传入不含全局修饰符时候的表现一样，不同的是有一个 lastIndex 属性。但是当正则表达式含有全局修饰符 g 时，正则会有一个 lastIndex 属性，下次的匹配会从 lastIndex 位置开始。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex">/\d/g</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> text <span class="token operator">=</span> <span class="token string">&quot;1plus2equals3&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> result<span class="token punctuation">;</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>result <span class="token operator">=</span> reg<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>
    <span class="token template-string"><span class="token string">`Matched </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>result<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> at position </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>result<span class="token punctuation">.</span>index<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">;next search begin at </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>
      reg<span class="token punctuation">.</span>lastIndex
    <span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">`</span></span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">//Matched 1 at position 0;next search begin at 1</span>
<span class="token comment">//Matched 2 at position 5;next search begin at 6</span>
<span class="token comment">//Matched 3 at position 12;next search begin at 13</span>
</code></pre></div><h2 id="es6-正则的扩展"><a href="#es6-正则的扩展" aria-hidden="true" class="header-anchor">#</a> ES6 正则的扩展</h2> <h3 id="u-修饰符"><a href="#u-修饰符" aria-hidden="true" class="header-anchor">#</a> u 修饰符</h3> <p>含义 Unicode 模式，用来正确处理大于\uFFFF 的字符。也就是可以正确处理 4 个字节的 UTF-16 编码</p> <p>点(.)字符含义是除了换行符以外的任意单个字符。对于码点大于 0xFFFF 的 Unicode 字符，点字符不能识别，必须加上 u 修饰符</p> <p>ES6 新增使用大括号表示 Unicode 字符表示法，这种表示法必须加上 u 修饰符才能识别大括号，否则会解读成量词。使用 u 修饰符之后，所以两次都会正确识别码点大于 0xFFFF 的 Unicode 字符</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token regex">/\u{61}/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token comment">//false     被认为是识别61个连续的u</span>
  a <span class="token operator">/</span>
  u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//true</span>
</code></pre></div><h3 id="y-修饰符"><a href="#y-修饰符" aria-hidden="true" class="header-anchor">#</a> y 修饰符</h3> <p>y 修饰符作用和 g 修饰符类似，也是全局匹配，后一次的匹配从上一次匹配成功的下一个位置开始。不同的是，g 修饰符只要剩余位置中存在匹配就行，而 y 修饰符会确保匹配必须从剩余的第一个位置开始。</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">&quot;aaa_aa_a&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> r1 <span class="token operator">=</span> <span class="token regex">/a+/g</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>r1<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//['aaa']</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>r1<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//['aa']</span>
<span class="token keyword">var</span> r2 <span class="token operator">=</span> <span class="token regex">/a+/y</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>r2<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//['aaa']</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>r2<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//null 剩余第一个位置是_,因此匹配失败</span>
</code></pre></div><h3 id="后行断言和后行否定断言"><a href="#后行断言和后行否定断言" aria-hidden="true" class="header-anchor">#</a> 后行断言和后行否定断言</h3> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">//后行断言</span>
<span class="token operator">/</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator">&lt;=</span>y<span class="token punctuation">)</span>x<span class="token operator">/</span>          <span class="token comment">//x只有在y的后面才匹配 匹配的是x</span>
<span class="token operator">/</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator">&lt;=</span>\$<span class="token punctuation">)</span>\d<span class="token operator">+</span><span class="token operator">/</span>       <span class="token comment">//只匹配美元后的数字</span>
<span class="token comment">//后行否定断言</span>
<span class="token operator">/</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator">&lt;</span><span class="token operator">!</span>y<span class="token punctuation">)</span>x<span class="token operator">/</span>           <span class="token comment">//只匹配x不再y后面</span>
<span class="token operator">/</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator">&lt;</span><span class="token operator">!</span>\$<span class="token punctuation">)</span>\d<span class="token operator">+</span><span class="token operator">/</span>        <span class="token comment">//只匹配不再$后面的数字</span>
</code></pre></div><h3 id="具名组匹配"><a href="#具名组匹配" aria-hidden="true" class="header-anchor">#</a> 具名组匹配</h3> <p>即给正则中的每个组起一个名字，如下</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">const</span> reg <span class="token operator">=</span> <span class="token regex">/(?&lt;year&gt;\d{4})-(?&lt;month&gt;\d{2})-(?&lt;day&gt;\d{2})/</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>reg<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">&quot;2018-01-11&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><img src="https://i.loli.net/2019/05/08/5cd2541ec31c6.jpg" alt></p> <p>具名组匹配的引用 \k&lt;组名&gt;,同时\1 依然有效且可以同时使用。 如下所示：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">const</span> reg <span class="token operator">=</span> <span class="token regex">/^(?&lt;word&gt;[a-z]+)!\k&lt;word&gt;$/</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>reg<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">&quot;abc!abc&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>reg<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">&quot;abc!ab&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//false</span>
<span class="token comment">// \k 和 \1 同时使用</span>
<span class="token keyword">const</span> reg <span class="token operator">=</span> <span class="token regex">/^(?&lt;word&gt;[a-z]+)!\k&lt;word&gt;!\1$/</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>reg<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">&quot;abc!abc!abc&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>reg<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">&quot;abc!abc!ab&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//false</span>
</code></pre></div><h2 id="一些好用的正则"><a href="#一些好用的正则" aria-hidden="true" class="header-anchor">#</a> 一些好用的正则</h2> <h3 id="时间格式化"><a href="#时间格式化" aria-hidden="true" class="header-anchor">#</a> 时间格式化</h3> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token class-name">Date</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">format</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">fmt</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> o <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;M+&quot;</span><span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token comment">//月份</span>
    <span class="token string">&quot;d+&quot;</span><span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">//日</span>
    <span class="token string">&quot;h+&quot;</span><span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getHours</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">//小时</span>
    <span class="token string">&quot;m+&quot;</span><span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">//分</span>
    <span class="token string">&quot;s+&quot;</span><span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getSeconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">//秒</span>
    <span class="token string">&quot;q+&quot;</span><span class="token punctuation">:</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">//季度</span>
    <span class="token constant">S</span><span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getMilliseconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//毫秒</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token regex">/(y+)/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>fmt<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    fmt <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>
      RegExp<span class="token punctuation">.</span>$<span class="token number">1</span><span class="token punctuation">,</span>
      <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getFullYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">4</span> <span class="token operator">-</span> RegExp<span class="token punctuation">.</span>$<span class="token number">1.</span>length<span class="token punctuation">)</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> k <span class="token keyword">in</span> o<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">&quot;(&quot;</span> <span class="token operator">+</span> k <span class="token operator">+</span> <span class="token string">&quot;)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>fmt<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>
        RegExp<span class="token punctuation">.</span>$<span class="token number">1</span><span class="token punctuation">,</span>
        RegExp<span class="token punctuation">.</span>$<span class="token number">1.</span>length <span class="token operator">==</span> <span class="token number">1</span> <span class="token operator">?</span> o<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token string">&quot;00&quot;</span> <span class="token operator">+</span> o<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">&quot;&quot;</span> <span class="token operator">+</span> o<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>length<span class="token punctuation">)</span>
      <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> fmt<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="url-参数获取"><a href="#url-参数获取" aria-hidden="true" class="header-anchor">#</a> url 参数获取</h3> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">const</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
location<span class="token punctuation">.</span>search<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/([^?&amp;=]+)=([^&amp;]+)/g</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">_<span class="token punctuation">,</span> k<span class="token punctuation">,</span> v</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">(</span>obj<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">=</span> v<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div> <div class="page"><section class="page-edit"><div><span data-flag-title="Your Article Title" class="leancloud-visitors"><em class="post-meta-item-text">阅读量： </em> <i class="leancloud-visitors-count"></i></span></div> <h3><a href="javascript:;"></a>
      评 论：
    </h3> <div id="vcomments"></div></section></div></div> <div class="page-edit"><!----> <!----></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/vuepress-blog/jsNotes/reflow.html" class="prev">
          回流（reflow）与重绘(repaint)
        </a></span> <span class="next"><a href="/vuepress-blog/jsNotes/arrSort.html">
          数组排序
        </a>
        →
      </span></p></div> </div> <div class="page"><section class="page-edit"><div><span data-flag-title="Your Article Title" class="leancloud-visitors"><em class="post-meta-item-text">阅读量： </em> <i class="leancloud-visitors-count"></i></span></div> <h3><a href="javascript:;"></a>
      评 论：
    </h3> <div id="vcomments"></div></section></div> <!----></div></div>
    <script src="/vuepress-blog/assets/js/app.21385443.js" defer></script><script src="/vuepress-blog/assets/js/10.b2b9db1b.js" defer></script>
  </body>
</html>
